package com.softplus.carrefour.hd.bltier.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.PrintWriter;
import java.io.IOException;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;

import com.epro.standard.StandardException;
import com.epro.standard.frontend.util.EproServletUtil;
import com.epro.standard.infrastructure.dataaccess.DBManage;

import com.softplus.carrefour.InfrastructureException;
import com.softplus.carrefour.hd.bltier.db.HibernateUtil;

public class uploadServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, 
                       HttpServletResponse response) throws ServletException, 
                                                            IOException {
        System.out.println("###########################\tuploadServlet\t########################");
        EproServletUtil servletUtil = new EproServletUtil(request);
        httpUpload upload;
        //        	DiskFileUpload dfUpload = new DiskFileUpload();
        //        	List items = new Vector();
        //			try {
        //				items = dfUpload.parseRequest(request);
        //			} catch (FileUploadException e1) {
        //				// TODO Auto-generated catch block
        //				e1.printStackTrace();
        //			}
        try {
            upload = new httpUpload(request);

            // Set Table Name
            upload.setTableName("CFHD_CASE_DOC");

            // Add primary key
            upload.addColumnPK("CASE_ID");
            upload.addColumnPK("CASE_DET_SEQ");
            upload.addColumnPK("CASE_DOC_SEQ");
            upload.addColumnPK("ATT_DESC");

            // Set file name column
            upload.setColumnFileName("ATT_FILE_NAME");

            // Add Value to PK
            Hashtable hash = upload.getFormFields();
            upload.addValuePK("'" + (String)hash.get("CASE_ID") + 
                              "'"); // Varchar
            upload.addValuePK((String)hash.get("CASE_DET_SEQ")); // Number
            upload.addValuePK((String)hash.get("CASE_DOC_SEQ")); // Number
            upload.addValuePK("'" + (String)hash.get("ATT_DESC") + 
                              "'"); // Varchar

            // Set file is BLOB or CLOB
            upload.setBLOB();
            //	         upload.setDataUnique("'"+caseId+"',"+detSeq+","+docSeq);

            // Set LOB Column
            upload.setLOBColumnName("ATT_FILE");

            // Set Destination is DATABASE or FILEBASE
            upload.setDestination("DATABASE");
            // (servletUtil.getRequestValue("action")).toLowerCase() ; 


            // Set Connection use package com.epro.standard.infrastructure.dataaccess.DBManage

            //					String connectionString = "jdbc:oracle:thin:@192.168.10.206:1521:db920";
            //					String user="CFHD";
            //					String password="CFHD";
            //					String driver = "oracle.jdbc.driver.OracleDriver";
            Connection con = null;
            //					DBManage db = new DBManage();

            //					try {
            try {
                con = HibernateUtil.getConnection();
                //						con = db.getConnection();
                //						Class.forName(driver);
                //						con = DriverManager.getConnection(connectionString,user,password); 
            } catch (InfrastructureException e3) {
                // TODO Auto-generated catch block
                e3.printStackTrace();
            }
            //					} catch (StandardException e) {
            //						// TODO Auto-generated catch block
            //						e.printStackTrace();
            //					}

            //					upload.setConnection((Connection) new DBManage().getConnection());
            upload.setConnection(con);

            try {
                upload.upload("kunlaporn_c");
            } catch (FileNotFoundException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            } catch (ServletException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            } catch (IOException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            } catch (SQLException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
        } catch (FileUploadException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}


